<?php
    ob_start();

    if (  isset($_GET['action']) and !empty($_GET['action']) and isset($_GET['task_id']) and !empty($_GET['task_id']) )
    {  
        switch ($_GET['action'])   
        {
            case "start_action":   t::status($_GET['task_id'], 1);      break;              
            case "run_action":     t::do_action( $_GET['task_id'] );    break;  
            case "stop_action":    t::status($_GET['task_id'], 0);      break;            
            case "remove_action":  t::remove($_GET['task_id']);         break;
            case "restart_action": 
                    t::status($_GET['task_id'], 1);
                   // t::$info[ $_GET['task_id']]['proc'] = array('cur' => 0, 'end' => 0); 
                   // t::$info[ $_GET['task_id']]['pro_alt'] = ''; 

                break;
        } 
    }


    if ( isset($_GET['action']) and $_GET['action'] == 'get')
    {

        echo t::get_list();  

        die();
    }
?>
<h2>Список задач</h2>
<style>
    .edit
    {
        font-size:9px;
    }
</style>
<script type="text/javascript">

    $(document).ready(function(){

        var stat = 0;
        var desc = 1;
        var res = $("#res"); 

        $('.fancybox').fancybox();

        <?php
            if ( setting::$info['check_task_log'] == 'false')
            {
                echo  "res.hide(); ";
            }
        ?>
        function cron_run()
        {
            var main = $("#main");     
            var pro = $("#pro");     

            var load = $("#load");  

            if (stat == 1)
                {
                $("#runner").fadeIn('slow');
                $.ajax({
                    type: "POST", 
                    url: "cron.php?type=json&add=task", 
                    data: "",
                    dataType: "json", 

                    success: function(data)
                    {  
                        pro.html( data.task);  
                        pro.off('click');
                        pro.on('click', '.restart_action, .run_action, .stop_action, .remove_action', function(){ clac(this); });

                        if ( desc == 1)
                            {
                            if (res.length > 1000000) 
                                {
                                res.empty();
                            }

                            res.append( data.file+"<br>");  

                        }
                        else
                            {
                            res.empty();
                        }

                        $("#runner").fadeOut('slow');   
                        setTimeout(cron_run, 1500);
                    },

                    error: function(data)
                    {
                        setTimeout(cron_run, 3000);  
                    },
                });
            }
        }

        function cron_run_action()
        {

            $.ajax({
                type: "POST", 
                url: "admin.php?page=task.list&action=get", 
                data: "",
                complete: function(data)
                {
                    result = data.responseText;  
                    $("#pro").html( result );
                }
            });

        }

        $("#start").click(
        function() 
        {
            stat = 1;
            $("#start").hide("slow");
            $("#stop").show("slow");

            cron_run();

        }
        );       

        $("#hide_desk").click(
        function() 
        {
            if ( desc == 1)
                {
                desc = 0;
                res.hide();
                res.empty();    
            }
            else
                {
                res.show();
                desc = 1; 
            }
        }
        );        


        $("#clean_desk").click(
        function() 
        {
            res.empty();
        }
        );

        $("#stop").click(function() {
            stat = 0;
            $("#start").show("slow");
            $("#stop").hide("slow");
        });


        $("div.main-text table.z tr:not(:first):odd").css("background", "#eee");    

        $(".task_run").click(
        function ()
        {
            $("#send_message").html('run');
            $("#load").fadeIn('slow');

            $.ajax({
                type: "POST", 
                url: "admin.php?page=task.list&type=ajax&action=run&topic_id="+this.id, 
                data: "",

                complete: function(data)
                {
                    $("#load").fadeOut('slow');
                }
            });
        }
        );

        $(".task_status").click(
        function ()
        {
            var main = $("#main");
            var load = $("#load");
            $("#send_message").html('Выполнение');
            load.fadeIn('slow');

            $.ajax({
                type: "POST", url: "admin.php?page=task.list&type=ajax&action=status&task_id="+this.id, data: "",

                complete: function(data)
                {
                    result = data.responseText;
                    main.html( result );
                    load.fadeOut('slow');
                }
            });
        }
        );     

        function clac(th)
        {
            var main = $("#main");
            var load = $("#load");
            var cl = th.className;
            load.fadeIn('slow'); 
            var ob = null;

            $.ajax({
                type: "POST", 
                url: "admin.php?page=task.list&type=json&action="+cl+"&task_id="+th.id, 
                data: "",
                dataType: "json", 
                success: function(data)
                {  
                    //  result = data.responseText;
                    //load.fadeOut('slow');
                    //  alert('ok');
                    //   ob = jQuery.parseJSON(result);

                    main.html( data.file); 
                },       
                complete: function(data)
                {
                    load.fadeOut('slow');   
                },
                error: function(data)
                {
                    alert('ошибка');
                    res.append( "Ошибка вызова"+"<br>");
                },
            });
        }

        $(".restart_action, .run_action, .start_action,  .stop_action, .remove_action").click(

        function (){

            clac(this);
        }

        );           

        $(".remove").click(
        function ()
        {
            var main = $("#main");
            var load = $("#load");
            $("#send_message").html('Удаление');
            load.fadeIn('slow');

            $.ajax({
                type: "POST", url: "admin.php?page=task.list&type=ajax&action=remove&id="+this.id, data: "",

                complete: function(data)
                {
                    result = data.responseText;
                    main.html( result );
                    load.fadeOut('slow');
                }
            });
        }
        );   

    });

</script>     
<div class="main-text" style="width:100%">  
    <a id="start" href="#">Пуск</a>  <a href="#" style="display: none;" id="stop">Стоп</a> <span style="display: none;" id="runner"><i>Выполняется..</i></span><br /><br />
    <div id="pro">
        <?php
            echo t::get_list();  
        ?>

    </div>  
    <br>
    <b>логи:</b> - <a id="hide_desk" href="#">Спрятать</a> ¦ <a id="clean_desk" href="#">Очистить</a><br><br>
    <div id="res" style="width: 100%; height: 200px; overflow:scroll;">

    </div>
</div>

<style>
    tr.head td{ 

        color: #444444;
        font-family: Tahoma,Arial,sans-serif;
        font-size: 11px;
        border-radius: 7px;
        border: 1px solid #DDDDDD;
        font-weight: bold;
    }

    #res
    {
        border-radius: 7px;
        border: 1px solid #DDDDDD;
        overflow-x:hidden;  
    }

</style>
<?php



    $t = ob_get_contents();
    ob_end_clean();    
    $array = array();
    $array['file'] = $t;
    if ( isset($_GET['type']) and $_GET['type'] == 'json')
    {
        echo json_encode($array);
    }
    else
    {
        echo $t;
    }
?>